package com.gxwz.medical.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gxwz.medical.dao.*;
import com.gxwz.medical.domain.Area;
import com.gxwz.medical.domain.ChronicInfo;
import com.gxwz.medical.domain.Payment;
import com.gxwz.medical.domain.Payperiod;
import com.gxwz.medical.domain.Person;
import com.gxwz.medical.exception.PayperiodUnfoundException;
import com.gxwz.medical.tools.DateUtil;
/**
 * 缴费管理控制模块
 * 
 * @author 演示
 * 
 */
public class PaymentServlet extends BaseServlet {
	private AreaDao areaDao;
	private PersonDao personDao;
	private static final String ADD_JSP = "/page/payment/payment_add.jsp";
	private static final String EDIT_JSP = "/page/payment/payment_edit.jsp";
	private static final String INPUT_JSP = "/page/payment/payment_input.jsp";
	private static final String CHOOSE_JSP = "/page/payment/payment_choose.jsp";
	private static final String MEMBERS_JSP = "/page/payment/payment_member.jsp";
	private static final String PRINT_JSP = "/page/payment/payment_print.jsp";
	
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}


	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String m = req.getParameter("m");// 请求处理动作类型:
		String url = ERROR_PATH;
		try {
			if ("input".equals(m)) {
				url = input(req, resp);
			}else if ("adds".equals(m)) {
				url = adds(req, resp);
			}else if ("get".equals(m)) {
				url = get(req, resp);
			}else if ("choose".equals(m)) {
				url = choose(req, resp);
			}else if ("members".equals(m)) {
				url = members(req, resp);
			}else if ("print".equals(m)) {
				url = print(req, resp);
			}else if ("pay".equals(m)) {
				url = pay(req, resp);
			}else if("del".equals(m)){
				this.del(req, resp);
			}
		}catch(PayperiodUnfoundException e){
        	e.printStackTrace();
        	req.setAttribute("error", e.getMessage());
        }catch (Exception e) {
			e.printStackTrace();
			req.setAttribute("error", e.getMessage());
		}
		// 跳转至目标页面
		process(req, resp, url);
	}



	private void del(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException{

	  	  //1：接收参数
	  	  String payNo=req.getParameter("id");
	  	  
	  	  //2:构造新慢病信息对象
	  	Payment model=new Payment();
	  	  try{
	  		  model.del(payNo);
	  		process(req, resp, "/page/payment/payment_input.jsp");
	  	  }catch(Exception e){
	  		  error(req, resp);
	  	  }
}
	





	private String get(HttpServletRequest req, HttpServletResponse resp) {
		// TODO Auto-generated method stub
		return EDIT_JSP;
	}


	private String adds(HttpServletRequest req, HttpServletResponse resp) {
		// TODO Auto-generated method stub
		return ADD_JSP;
	}


	/**
	 * 缴费初始化界面
	 * @param req
	 * @param resp
	 * @return
	 * @throws Exception
	 */
	private String input(HttpServletRequest req, HttpServletResponse resp)
			throws Exception {
        //初始化村列表
		String town=(String)req.getSession().getAttribute("agencode");
		List<Area> villages=areaDao.findChildArea(town, 3);
		req.setAttribute("villages", villages);
		//初始化村列表第一个元素对应的组列表
	    if(villages!=null&&!villages.isEmpty()){
	    	Area area=villages.get(0);
	    	String groupNo=area.getAreacode();
	    	List<Area> groups=areaDao.findChildArea(groupNo, 4);
	    	req.setAttribute("groups", groups);
	    }
		return INPUT_JSP;
	}
	/**
	 * 查找户主信息
	 * @param req
	 * @param resp
	 * @return
	 * @throws Exception
	 */
	private String choose(HttpServletRequest req, HttpServletResponse resp)
			throws Exception {
		//村编号
		String village=req.getParameter("village");
		//组编号
		String group=req.getParameter("group");
		//户主姓名
		String persname=req.getParameter("persname");
		//行政区域编号默认等于村编号
		String areacode=village;
		//如果组编号不为空，行政区域编号等于组编号
		if(group!=null&&!"".equals(group)){
			areacode=group;
		}
		//根据村编号+组编号+户主编号查找户主信息
		List<Person> hostList=personDao.findHostByName(persname, areacode);
		req.setAttribute("hostList", hostList);
		return CHOOSE_JSP;
		
	}
	/**
	 * 查询未交费人员列表
	 * @param req
	 * @param resp
	 * @return
	 * @throws Exception
	 */
	private String members(HttpServletRequest req, HttpServletResponse resp)
			throws Exception {
		//缴费年份，默认为当前系统时间对应的年份
		int payYear=DateUtil.getYear(new java.util.Date());
		String famicode=req.getParameter("famicode");
		//根据家庭编号+缴费年度查找未缴费人员名单
        List<Person> persons=personDao.findUnpayList(famicode, payYear);
        //根据年份读取并显示缴费标准
        Payperiod payperiod=new Payperiod(payYear);
        req.setAttribute("amount", payperiod.getAmount());
        req.setAttribute("persons", persons);
		return MEMBERS_JSP;
	}
	/**
	 * 缴费过程
	 * @param req
	 * @param resp
	 * @return
	 * @throws Exception
	 */
	private String pay(HttpServletRequest req, HttpServletResponse resp)
			throws Exception {
		//获取选中的缴费成员名单
        String[] perscodes=req.getParameterValues("perscodes");
        Payment payment=new Payment();
        String creator=(String)req.getSession().getAttribute("userName");
        //缴费年份，默认为当前系统时间对应的年份
      	int payYear=DateUtil.getYear(new java.util.Date());
      	//缴费时间设置
        Payperiod payperiod=new Payperiod(payYear);
        //执行缴费过程
        payment.addPay(perscodes, payperiod, creator);
        
		return input(req, resp);
	}
	private String print(HttpServletRequest req, HttpServletResponse resp)
			throws Exception {

		return PRINT_JSP;
	}

	@Override
	public void init() throws ServletException {
		 areaDao=new AreaDao();
		 personDao=new PersonDao();
	}

}
